{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "81e3d65c",
   "metadata": {},
   "source": [
    "--------------------\n",
    "\n",
    "* 本周主要内容：语言识别\n",
    "* 22春API人工智能与机器学习_week11\n",
    "* 学习人：罗海鑫\n",
    "<br>\n",
    "<br>\n",
    "\n",
    "--------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3855b525",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9eda5e23",
   "metadata": {},
   "source": [
    "# 2.关于麦克风包安装\n",
    "\n",
    "* 安装speech_recognition\n",
    "\n",
    "> pip install SpeechRecognition\n",
    "\n",
    "* 安装PyAudio\n",
    "\n",
    "> Not foun name \"PyAudio\" moudle\n",
    "\n",
    "> pip install pipwin\n",
    "\n",
    "> pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01ddcc7d",
   "metadata": {},
   "source": [
    "# 3.优化ASR(自动语音识别）机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2487c229",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 百度密钥\n",
    "API_KEY = 'Gd8ErpvyxUzWBF1Wo9ewgKC7'\n",
    "SECRET_KEY = '9vEF06r9eigjx3Gi8YyaLz4EGPAt41f2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "58ed2c34",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a69e9e92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.f5ffcfc5e58dfc821883aca56173bf56.2592000.1655346111.282335-26194926 ; EXPIRES IN SECONDS: 2592000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'24.f5ffcfc5e58dfc821883aca56173bf56.2592000.1655346111.282335-26194926'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "luo_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "luo_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f61f82d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备音频文件\n",
    "\n",
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc44aae6",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr_output = eval(asr(luo_token,'1.wav'))['result'][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18c6239f",
   "metadata": {},
   "source": [
    "# 4.连接图灵机器人\n",
    "\n",
    "* API V2.0是基于图灵机器人平台语义理解、深度学习等核心技术，为广大开发者和企业提供的在线服务和开发接口。\n",
    "\n",
    "* 接口地址：\n",
    "> http://openapi.turingapi.com/openapi/api/v2\n",
    "\n",
    "* 请求参数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e55c496e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab13829f",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 学生实践：中英翻译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "260b5c06",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_KEY = \"\"\n",
    "def TL_URL(key,text)\n",
    "TL_URL = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "\n",
    "payload={\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"附近的酒店\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": \"\",\n",
    "        \"userId\": \"\"\n",
    "    }\n",
    "}\n",
    "res = requests.post(TL_URL,data = json.dumps(payload))\n",
    "return res.json()['result'][0]['values']['text']\n",
    "# dict 不等于 json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be20c6f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f5cbd7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "res.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe2be504",
   "metadata": {},
   "source": [
    "## 4.1接入对话机器人的流程\n",
    "\n",
    "> 1.录制音频\n",
    "> 2. 语音识别--（百度ASR接口）--- 音频转文本信息\n",
    "> 3. 图灵API--（进行文字输入和文字回复）\n",
    "\n",
    "> 4. (待做)语音合成（文字信息输出音频）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72bc18c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.录制音频\n",
    "\n",
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "\n",
    "# 2. 语音识别\n",
    "luo_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(luo_token,'1.wav'))['result'][0]\n",
    "\n",
    "# 3.图灵API\n",
    "TL_API(TL_KEY,asr_output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a2c8076",
   "metadata": {},
   "source": [
    "# 5.语音合成测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c5a8d53",
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "import sys\n",
    "import json\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "    from urllib.parse import quote_plus\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib import quote_plus\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "API_KEY = '4E1BG9lTnlSeIf1NQFlrSq6h'\n",
    "SECRET_KEY = '544ca4657ba8002e3dea3ac2f5fdd241'\n",
    "\n",
    "TEXT = \"欢迎使用百度语音合成。\"\n",
    "\n",
    "# 发音人选择, 基础音库：0为度小美，1为度小宇，3为度逍遥，4为度丫丫，\n",
    "# 精品音库：5为度小娇，103为度米朵，106为度博文，110为度小童，111为度小萌，默认为度小美 \n",
    "PER = 1\n",
    "# 语速，取值0-15，默认为5中语速\n",
    "SPD = 5\n",
    "# 音调，取值0-15，默认为5中语调\n",
    "PIT = 5\n",
    "# 音量，取值0-9，默认为5中音量\n",
    "VOL = 5\n",
    "# 下载的文件格式, 3：mp3(default) 4： pcm-16k 5： pcm-8k 6. wav\n",
    "AUE = 3\n",
    "\n",
    "FORMATS = {3: \"mp3\", 4: \"pcm\", 5: \"pcm\", 6: \"wav\"}\n",
    "FORMAT = FORMATS[AUE]\n",
    "\n",
    "CUID = \"123456PYTHON\"\n",
    "\n",
    "TTS_URL = 'http://tsn.baidu.com/text2audio'\n",
    "\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "SCOPE = 'audio_tts_post'  # 有此scope表示有tts能力，没有请在网页里勾选\n",
    "\n",
    "\n",
    "# def fetch_token():\n",
    "#     print(\"fetch token begin\")\n",
    "#     params = {'grant_type': 'client_credentials',\n",
    "#               'client_id': API_KEY,\n",
    "#               'client_secret': SECRET_KEY}\n",
    "#     post_data = urlencode(params)\n",
    "#     if (IS_PY3):\n",
    "#         post_data = post_data.encode('utf-8')\n",
    "#     req = Request(TOKEN_URL, post_data)\n",
    "#     try:\n",
    "#         f = urlopen(req, timeout=5)\n",
    "#         result_str = f.read()\n",
    "#     except URLError as err:\n",
    "#         print('token http response http code : ' + str(err.code))\n",
    "#         result_str = err.read()\n",
    "#     if (IS_PY3):\n",
    "#         result_str = result_str.decode()\n",
    "\n",
    "#     print(result_str)\n",
    "#     result = json.loads(result_str)\n",
    "#     print(result)\n",
    "#     if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "#         if not SCOPE in result['scope'].split(' '):\n",
    "#             raise DemoError('scope is not correct')\n",
    "#         print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))\n",
    "#         return result['access_token']\n",
    "#     else:\n",
    "#         raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    token = fetch_token()\n",
    "    tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode\n",
    "    print(tex)\n",
    "    params = {'tok': token, 'tex': tex, 'per': PER, 'spd': SPD, 'pit': PIT, 'vol': VOL, 'aue': AUE, 'cuid': CUID,\n",
    "              'lan': 'zh', 'ctp': 1}  # lan ctp 固定参数\n",
    "\n",
    "    data = urlencode(params)\n",
    "    print('test on Web Browser' + TTS_URL + '?' + data)\n",
    "\n",
    "    req = Request(TTS_URL, data.encode('utf-8'))\n",
    "    has_error = False\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "\n",
    "        headers = dict((name.lower(), value) for name, value in f.headers.items())\n",
    "\n",
    "        has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "        has_error = True\n",
    "\n",
    "    save_file = \"error.txt\" if has_error else 'result.' + FORMAT\n",
    "    with open(save_file, 'wb') as of:\n",
    "        of.write(result_str)\n",
    "\n",
    "    if has_error:\n",
    "        if (IS_PY3):\n",
    "            result_str = str(result_str, 'utf-8')\n",
    "        print(\"tts api  error:\" + result_str)\n",
    "\n",
    "    print(\"result saved as :\" + save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11e0b413",
   "metadata": {},
   "source": [
    "# 6.在jupyter 中播放音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e70b4a64",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef1b8bb5",
   "metadata": {},
   "source": [
    "# 7.智能对话机器人最终版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1bfcf66",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition # 语音音频录制模块\n",
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play # 播放音频模块\n",
    "from bdasr import fetch_token,asr # token和语音识别模块\n",
    "from bdtts import tts # 语音合成模块\n",
    "\n",
    "# 1.录制音频\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "\n",
    "# 2. 语音识别\n",
    "luo_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(luo_token,'1.wav'))['result'][0]\n",
    "\n",
    "# 3.图灵API\n",
    "TL_result = TL_API(TL_KEY,asr_output)\n",
    "\n",
    "# 4.语音合成TTS --> 产生result.wav\n",
    "tts(luo_token,TL_result)\n",
    "\n",
    "# 5.在jupyter中进行音频播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab73015c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
